<?php

	
	mb_internal_encoding('UTF-8');
    set_time_limit ( 60 * 60 * 5 );

	//script configuration
	error_reporting(E_ALL);
	ini_set('display_errors', TRUE);
	date_default_timezone_set('Europe/London');
	header('Content-Type: text/html; charset=utf-8');
	
	//*
// Define path to application directory
defined('LIBRARY_PATH')
    || define('LIBRARY_PATH', realpath(dirname(__FILE__) . '/../library'));
	
	
	set_include_path(
	implode(PATH_SEPARATOR, array(LIBRARY_PATH, get_include_path()))); 
	
	
	
	// Set up Zend Autoloader	
	require_once 'Zend/Loader/Autoloader.php';	
	$autoloader = Zend_Loader_Autoloader::getInstance();	
	$autoloader->registerNamespace('Dealscombined_');
		
	//database settings
	$dataBase = new Zend_Db_Adapter_Pdo_Mysql(array(
	        'host'     => 'ec2-54-247-19-167.eu-west-1.compute.amazonaws.com',
	        'username' => 'superuser',
	        'password' => '2055',
	        'dbname'   => 'dealscombined'
    	));
	
	$dataBase->getConnection()->exec('DROP TABLE IF EXISTS products_temp');
	$dataBase->query('CREATE TABLE products_temp like products');    
   	
	$wowcherFeedUrl      = 'http://datafeed.api.productserve.com/datafeed/download/apikey/a2e7c9aeaed77685f8c4e1f951d79686/mid/3595/columns/merchant_id,merchant_name,aw_product_id,merchant_product_id,product_name,description,category_id,category_name,merchant_category,aw_deep_link,aw_image_url,search_price,currency,delivery_cost,merchant_deep_link,merchant_image_url,aw_thumb_url,brand_id,brand_name,commission_amount,commission_group,condition,delivery_time,display_price,ean,in_stock,is_hotpick,isbn,is_for_sale,language,merchant_thumb_url,model_number,mpn,parent_product_id,pre_order,product_type,promotional_text,rrp_price,specifications,stock_quantity,store_price,upc,valid_from,valid_to,warranty,web_offer/format/xml/compression/zip/'; 
    $livingSocialFeedUrl = 'http://datafeed.api.productserve.com/datafeed/download/apikey/a2e7c9aeaed77685f8c4e1f951d79686/mid/3925/columns/merchant_id,merchant_name,aw_product_id,merchant_product_id,product_name,description,category_id,category_name,merchant_category,aw_deep_link,aw_image_url,search_price,currency,delivery_cost,merchant_deep_link,merchant_image_url,aw_thumb_url,brand_id,brand_name,commission_amount,commission_group,condition,delivery_time,display_price,ean,in_stock,is_hotpick,isbn,is_for_sale,language,merchant_thumb_url,model_number,mpn,parent_product_id,pre_order,product_type,promotional_text,rrp_price,specifications,stock_quantity,store_price,upc,valid_from,valid_to,warranty,web_offer/format/xml/compression/zip/';
    $kgbFeedUrl          = 'http://datafeed.api.productserve.com/datafeed/download/apikey/a2e7c9aeaed77685f8c4e1f951d79686/mid/2965/columns/merchant_id,merchant_name,aw_product_id,merchant_product_id,product_name,description,category_id,category_name,merchant_category,aw_deep_link,aw_image_url,search_price,currency,delivery_cost,merchant_deep_link,merchant_image_url,aw_thumb_url,brand_id,brand_name,commission_amount,commission_group,condition,delivery_time,display_price,ean,in_stock,is_hotpick,isbn,is_for_sale,language,merchant_thumb_url,model_number,mpn,parent_product_id,pre_order,product_type,promotional_text,rrp_price,specifications,stock_quantity,store_price,upc,valid_from,valid_to,warranty,web_offer/format/xml/compression/zip/';
	
	//Creating different sites processors
	$wowcherProcessor      = new Dealscombined_Crawlers_Wowcher_SiteProcessor($dataBase, null, $wowcherFeedUrl, LIBRARY_PATH.'/../data/feeds/wowcher.xml');
	$livingSocialProcessor = new Dealscombined_Crawlers_LivingSocial_SiteProcessor($dataBase, null, $livingSocialFeedUrl, LIBRARY_PATH.'/../data/feeds/livingSocial.xml');
	$kgbProcessor          = new Dealscombined_Crawlers_Kgb_SiteProcessor($dataBase, null, $kgbFeedUrl, LIBRARY_PATH.'/../data/feeds/kgb.xml');
	
	//Adding differente site processors to the site iterator and running it
	$siteIterator   = new Dealscombined_SiteIterator_Iterator;
	$siteIterator->addProcessor($wowcherProcessor);
	$siteIterator->addProcessor($livingSocialProcessor);
	$siteIterator->addProcessor($kgbProcessor); 
	$siteIterator->run();
	
	//Trying to force a city id based on other attributes
	$dataBase->query("UPDATE products_temp SET city_id = (SELECT id from dc_cities WHERE products_temp.merchant_deep_link COLLATE UTF8_GENERAL_CI LIKE CONCAT ('%', dc_cities.name, '%') ORDER BY name ASC LIMIT 1) WHERE city_id IS NULL");
	$dataBase->query("UPDATE products_temp SET city_id = (SELECT id FROM dc_cities WHERE products_temp.warranty COLLATE UTF8_GENERAL_CI LIKE CONCAT('%', dc_cities.name ,'%') OR products_temp.specifications LIKE CONCAT('%', dc_cities.name ,'%') ORDER BY name ASCLIMIT 1) WHERE city_id IS NULL");
	$dataBase->query('UPDATE products_temp SET city_id = (SELECT id from dc_cities WHERE products_temp.specifications = dc_cities.name LIMIT 1) WHERE city_id IS NULL');	 
	$dataBase->query("UPDATE products_temp SET city_id = '28' WHERE city_id IS NULL");
	
	//Replacing old database
	$dataBase->query('TRUNCATE cities');	
	$dataBase->query('INSERT INTO cities (id, name) SELECT p.city_id, c.name from products_temp AS p, dc_cities AS c WHERE p.city_id = c.id GROUP BY city_id ORDER BY name ASC');
	$dataBase->getConnection()->exec('DROP TABLE IF EXISTS products_old');
	$dataBase->query('RENAME TABLE products TO products_old');
	$dataBase->query('RENAME TABLE products_temp TO products');